<?php
// +----------------------------------------------------------------------
// | When work is a pleasure, life is a joy!
// +----------------------------------------------------------------------
// | User: ShouKun Liu  |  Email:24147287@qq.com  | Time:2017/4/27 22:17
// +----------------------------------------------------------------------
// | TITLE: 佣金
// +----------------------------------------------------------------------

namespace Qwadmin\Controller;


use Common\Model\OrderCommissionModel;
use Common\Model\OrderModel;
use Think\Page;

class CommissionController extends ComController
{
    protected $money1;
    protected $money2;
    protected $OrderCommission;
    protected $order;


    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        $this->money1 = C('USER1MONEY');
        $this->money2 = C('USER2MONEY');
        $this->OrderCommission = new OrderCommissionModel();
        $this->order = new  OrderModel();
    }

    /**
     * 获取用户列表
     * @return mixed
     */
    public static function getMember()
    {
        return M('member')->select();
    }


    /**
     * 分成列表
     */
    public function lists()
    {
        if ($this->USER['user'] != 'admin') {
            $where['username'] = $this->USER['user'];
        } else {
            $username = I('username', '');
            if (!empty($username)) $where['username'] = $username;
            $this->assign('member', self::getMember());
        }
        //获取当天
        $now = date('Y-m-d');
        $rq = I('date', $now);
        if (empty($rq)) return false;
        $times = strtotime($rq);
        //开始时间
        $startDate = date('Y-m-d 00:00:00', $times);
        $endDate = date('Y-m-d 23:59:59', $times);
        $where['create_at'] = array(array('gt', $startDate), array('elt', $endDate), 'and');
        $count = $this->OrderCommission->where($where)->order('id desc')->count();
        $Page = new Page($count, 30);
        $dataList = $this->OrderCommission->where($where)->order('id desc')
            ->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('dataList', $dataList);
        $this->assign('page', $Page->show());


        //统计当天改客服总提成
        if (!empty($username)) $where['username'] = $username;
        $money = $this->OrderCommission->where($where)->sum('money');
        $this->assign('money', $money);
        $this->assign('user', $this->USER['user']);
        $this->display('lists');
    }

    public function edit()
    {

        $id = I('id', '');
        if (empty($id)) $this->error('错误1', U('lists'));
        $OrderCommissionModel = new OrderCommissionModel();
        $model = $OrderCommissionModel->find($id);
        if (empty($model)) $this->error('错误2', U('lists'));
        if (IS_POST) {

            $data = I('');
            $OrderCommissionModel->save($data);
            $this->success('修改成功', U('lists'));
        } else {
            $this->assign('model', $model);
            $this->display();
        }

    }


    public function todoCommission()
    {
        //获取当天
        $rq = I('date');
        if (empty($rq)) return false;
        $times = strtotime($rq);
        //开始时间
        $startDate = date('Y-m-d 00:00:00', $times);
        $endDate = date('Y-m-d 23:59:59', $times);

        //查询订单
        $order = $this->order;
        $wheres['create_date'] = array(array('gt', $startDate), array('elt', $endDate), 'and');
        $wheres['status'] =array('in',array(3,6,7));
        $wheres['is_aftersale'] =2;
        $wheres['is_commission'] = 0;
        $orderList = $order->where($wheres)->select();

        //开始处理订单
        $dataList =array();
        foreach ($orderList as $orderOne) {
            $dataOne = $this->addCommission($orderOne);
            $dataList = array_merge($dataList, $dataOne);
            $orderIds[] = $orderOne['id'];
        }
        if (!empty($dataList)) {
            $this->OrderCommission->addAll($dataList);
            $where['id'] = array('in', $orderIds);
            //更新订单状态
            $this->order->data(array('is_commission' => 1))->where($where)->save(false);
        }
        //添加数据
        $this->success();
    }

    /**
     * 添加分成
     * @param $orderOne
     * @return array
     */
    protected function addCommission($orderOne)
    {
        $dataList = array();
        //客服1
        if (!empty($orderOne['dispose_user'])) {
            $commissionModel['username'] = $orderOne['dispose_user'];
            $commissionModel['order_id'] = $orderOne['id'];
            $commissionModel['money'] = round(number_format($this->money1, 2), 2);
            $commissionModel['create_at'] = date('Y-m-d H:i:s');
            $commissionModel['create_at'] = $orderOne['create_date'];
            $dataList[] = $commissionModel;
        }

        if (!empty($orderOne['dispose_user2'])) {
            $commissionModel2['username'] = $orderOne['dispose_user2'];
            $commissionModel2['order_id'] = $orderOne['id'];
            $commissionModel2['money'] = round(number_format($this->money2, 2), 2);
            $commissionModel2['create_at'] = $orderOne['create_date'];
            $dataList[] = $commissionModel2;
        }

        //添加数据
//        $this->OrderCommission->add($commissionModel2);

        return $dataList;
        //
    }


}